package cn.shyouchi.wuguanjia.android.controls;

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;

/**
 * Created by movier on 9/21/16.
 */

public class AdvanceEditDialogFragment extends DialogFragment {

    @BindView(R.id.advance_edit_dialog_title)
    TextView title;
    @BindView(R.id.advance_edit_dialog_subtitle)
    TextView subtitle;
    @BindView(R.id.advance_edit_dialog_edit_text_1)
    EditText editText1;
    @BindView(R.id.advance_edit_dialog_edit_text_2)
    EditText editText2;

    private static final String TAG = "AdvanceEditDialog";

    private static final String ARG_TITLE = "title";
    private static final String ARG_SUBTITLE = "subtitle";
    private static final String ARG_HINT_ONE = "hint1";
    private static final String ARG_HINT_TWO = "hint2";

    private String mTitle, mSubtitle, hint1, hint2;

    public interface AdvanceEditDialogListener {
        void onAdvanceEditDialogPositiveClick(DialogFragment dialog, String content1, String content2);
    }

    AdvanceEditDialogListener mListener;


    public void setClickListener(AdvanceEditDialogListener mListener) {
        this.mListener = mListener;
    }


    public static AdvanceEditDialogFragment newInstance(
            String title, String subtitle, String contentHint1, String contentHint2) {
        AdvanceEditDialogFragment fragment = new AdvanceEditDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putString(ARG_SUBTITLE, subtitle);
        args.putString(ARG_HINT_ONE, contentHint1);
        args.putString(ARG_HINT_TWO, contentHint2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTitle = getArguments().getString(ARG_TITLE);
            mSubtitle = getArguments().getString(ARG_SUBTITLE);
            hint1 = getArguments().getString(ARG_HINT_ONE);
            hint2 = getArguments().getString(ARG_HINT_TWO);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
//        try {
//            mListener = (AdvanceEditDialogListener) activity;
//        } catch (ClassCastException e) {
//            // The activity doesn't implement the interface, throw exception
//            throw new ClassCastException(activity.toString()
//                    + " must implement AdvanceEditDialogListener");
//        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_fragment_advance_edit, container, false);
        ButterKnife.bind(this, view);

        title.setText(mTitle);
        subtitle.setText(mSubtitle);
        editText1.setHint(hint1);
        editText2.setHint(hint2);
        return view;
    }

    @OnClick(R.id.advance_edit_dialog_confirm_button)
    void confirmButtonTapped() {
        if (editText1.getText().toString().isEmpty() || editText2.getText().toString().isEmpty()) {
            Toast.makeText(getActivity(), "内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (mListener != null) {
            mListener.onAdvanceEditDialogPositiveClick(this, editText1.getText().toString(), editText2.getText().toString());
        }
        getDialog().cancel();
    }

    @OnClick(R.id.advance_edit_dialog_cancel_button)
    void cancelButtonTapped() {
        getDialog().cancel();
    }
}
